package com.qxzn.module.agora;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.EventBusBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.widget.statusbar.StatusBar;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.Constant;
import com.qxzn.app.util.SharePreferenceUtil;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 收到视频通话请求
 */
public class VideoChatReceiveActivity extends BaseActivity {
    @BindView(R2.id.iv_head)
    ImageView ivHead;
    @BindView(R2.id.tv_user)
    TextView tvUser;
    @BindView(R2.id.tv_status)
    TextView tvStatus;
    @BindView(R2.id.rl_head)
    RelativeLayout rlHead;

    @Override
    protected int initLayout() {
        return R.layout.activity_video_chat_receive;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
        title_bar.setVisibility(View.GONE);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        Glide.with(this).load(HttpRequest.BASE_URL + getIntent().getStringExtra("headPortrait")).apply(RequestOptions.circleCropTransform()
                .placeholder(R.mipmap.icon_xiaopai).error(R.mipmap.icon_xiaopai)).into(ivHead);
        tvUser.setText(getIntent().getStringExtra("nickname"));
    }

    @Override
    protected void setStatusBarColor() {
        StatusBar.setStatusBarLightMode(this, getResources().getColor(R.color.black_333333));
    }

    @OnClick({R2.id.iv_hungup, R2.id.iv_answer})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.iv_hungup) {
            requestVideoChat(5, 1);
            finish();
        } else if (i == R.id.iv_answer) {
            startActivity(getIntent().setClass(this, VideoChatCallActivity.class));
            finish();
        }
    }

    @Override
    public void onEventBus(EventBusBean busBean) {
        super.onEventBus(busBean);
        if (busBean.getId() == Constant.EventBusId.HUNG_UP_VIDEO) {
            if (busBean.getIntMsg() == 0) {//对方已取消
                if (TextUtils.equals(getIntent().getStringExtra("sender"), busBean.getMsg())) {//如果是请求拨打方是当前来电用户，则关闭页面
                    AppUtil.showToast(this, "对方已取消");
                    finish();
                }
            }
        }
    }

    private void requestVideoChat(int code, int refuseType) {
        JSONObject params = new JSONObject();
        params.put("sender", SharePreferenceUtil.getUserName(this));//发送者，桌面版：sender为机器码，APP：sender为手机号
        params.put("number", SharePreferenceUtil.getDeviceId(this));//接收者的手机号
        params.put("code", code);//1视频通话 2监控
        if (code == 5) {
            params.put("refusetype", refuseType);//挂断类型：0：取消拨打；1：拒绝接听
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        Call<BaseBean> call = HttpRequest.getService().requestVideoChat(body);
        call.enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                BaseBean bean = response.body();
                if (bean != null && bean.getCode() == 200) {
                    AppUtil.showLog("call video communication success");
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                AppUtil.showToast(VideoChatReceiveActivity.this, "网络错误，请稍后再试！");
            }
        });
    }

}
